<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2013, Chongqing Zero Technology Co. Ltd.
 * @link           http://www.xuewl.com
**/
namespace Admin\Controller;
Class SettingController extends InitController {
    public function _initialize() {
    	parent::_initialize();
        $this->db = D('Setting');
        $this->groupConfig = array(
            'base'   => '基本设置',
            'safe'   => '安全设置',
            'upload' => '上传设置',
            'email'  => '邮箱设置',
            'other'  => '其它设置',
        );
        $this->groupid = getgpc('groupid');
        $this->groupid = (isset($this->groupConfig[$this->groupid])) ? $this->groupid : 'base';
    }

    /* 角色管理 */
    public function init() {
        $setting = $this->db->getField('key,value');
        $form = new \Common\Library\form();
        $style_list = template_list(0);
        foreach ($style_list as $k=>$v) {
            $style_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
            unset($style_list[$k]);
        }
		include $this->admin_tpl('setting');
    }

    /**
     * 保存配置
     * @author xuewl <master@xuewl.com>
     */
    public function update() {
        if (submitcheck('dosubmit')) {
            $info = $_POST['setting'];
            if (empty($info)) {
                $this->error('参数错误');
            }
            if (empty($info['authkey'])) {
                $info['authkey'] = random(30);
            }
            foreach ($info as $k => $v) {
                $this->db->where(array('key' => $k))->setField('value', $v);
            }
            @file_put_contents(CONF_PATH.'setting.php', "<?php \n return ".array2string(array_change_key_case($info,CASE_UPPER)).";\n?>");
            $this->success('操作成功');
        } else {
            $this->error('请勿非法提交');
        }
    }
    /**
     * 字段值
     * @param  string $fieldtype 字段类型
     * @param  string $key       字段名
     * @author xuewl <master@xuewl.com>
     */
    public function public_field_setting($fieldtype = 'text', $key='') {
        if (!empty($key)) {
            $data = (array) $this->setting_db->find($key);
            $data['config'] = unserialize($data['config']);
            $this->assign('data', $data);
        }
        $result = '';
        $result['field_minlength'] = 0;
        $result['field_maxlength'] = '';
        $result['setting'] = $this->fetch('_util_field_'.$fieldtype);
        $this->ajaxReturn($result);
    }

    /**
     * 添加拓展字段
     * @author xuewl <master@xuewl.com>
     */
    public function field_add() {
        if (submitcheck('dosubmit')) {
            $info = $_POST;
            $result = $this->setting_db->update($info);
            if (!$result) {
                $this->error($this->stting_db->getError());
            }
            $this->success('操作成功');
        } else {
            $this->display();
        }
    }

    /**
     * 编辑拓展字段
     * @param  string $key 字段名
     * @author xuewl <master@xuewl.com>
     */
    public function field_edit($key) {
        $data = $this->setting_db->detail_by_key($key);
        $data['config'] = unserialize($data['config']);
        if (submitcheck('dosubmit')) {
            $result = $this->setting_db->update($_POST, 'update');
            if (!$result) {
                $this->error($this->setting_db->getError());
            }
            $this->success('操作成功');
        } else {
            $this->assign('data', $data);
            $this->display();
        }
        print_r($data);
    }

    /* 字段列表 */
    public function field_lists() {
        $data = $this->setting_db->order("listorder ASC")->select();
        $this->assign('data', $data);
        $this->display();
    }

    /* 删除字段 */
    public function field_delete() {

    }

    /**
     * 字段排序
     * @author xuewl <master@xuewl.com>
     */
    public function public_listorder() {        
        if (submitcheck('dosubmit')) {
            $listorders = (array) $_POST['listorders'];
            if (empty($listorders)) {
                $this->error('数据非法');
            }
            foreach ($listorders as $key => $value) {
                $this->setting_db->save(array('key' => $key, 'listorder' => (int) $value));
            }
            $this->success('操作成功');
        } else {
            $this->error('请勿非法提交');
        }
    }

    /**
     * 发送测试邮件
     * @author xuewl <master@xuewl.com>
     */
    public function public_test_mail() {
        helpers('mail');
        $subject = 'tpcms test mail';
        $message = 'this is a test mail from tpcms team';
        $mail = array (
            'mailsend' => 2,
            'maildelimiter' => 1,
            'mailusername' => 1,
            'server' => $_POST['mail_server'],
            'port' => intval($_POST['mail_port']),
            'mail_type' => intval($_POST['mail_type']),
            'auth' => intval($_POST['mail_auth']),
            'from' => $_POST['mail_from'],
            'auth_username' => $_POST['mail_user'],
            'auth_password' => $_POST['mail_password']
        );    
        if(sendmail($_POST['mail_to'],$subject,$message,$_POST['mail_from'],$mail)) {
            echo L('test_email_succ').$_POST['mail_to'];
        } else {
            echo L('test_email_faild');
        }
    }

    /**
     * 检测GD库
     * @author xuewl <master@xuewl.com>
     */
    private function check_gd() {
        if(!function_exists('imagepng') && !function_exists('imagejpeg') && !function_exists('imagegif')) {
            $gd = L('gd_unsupport');
        } else {
            $gd = L('gd_support');
        }
        return $gd;
    }
}